package com.hazens.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AudioManager.OnAudioFocusChangeListener, View.OnClickListener{

    private final String TAG = MainActivity.class.getSimpleName();
    private AudioManager audioManager;
    public HandlerThread mHandlerThread = new HandlerThread(TAG);
    private RadarAlarmHandler radarAlarmHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*int uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
//                | View.SYSTEM_UI_FLAG_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        getWindow().getDecorView().setSystemUiVisibility(uiFlags);*/
//        getWindow().addFlags(uiFlags);
        Log.d(TAG, "onCreate: ");
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        initHandler();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AOAConnectManager.getInstance().stopAOAReadThread();
        AOAConnectManager.getInstance().stopAOAConnectThread();
        AOAConnectManager.getInstance().stopSocketReadThread();
    }

    private void initHandler() {
        mHandlerThread.start();
        radarAlarmHandler = new RadarAlarmHandler(mHandlerThread.getLooper());
    }

    @Override
    public void onAudioFocusChange(int focusChange) {
        Log.d(TAG, "onAudioFocusChange: focusChange=" + focusChange);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.request_audio_focus:
//                audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                radarAlarmHandler.sendEmptyMessage(0);
                break;
            case R.id.abandon_audio_focus:
//                audioManager.abandonAudioFocus(this);
                radarAlarmHandler.sendEmptyMessage(1);
                break;
            case R.id.send1:
                radarAlarmHandler.sendEmptyMessage(2);
                break;
            case R.id.send2:
                radarAlarmHandler.sendEmptyMessage(3);
                break;
        }
    }


    class UsbReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "onReceive: action=" + action);
            Toast.makeText(context, "onReceive: action=" + action, Toast.LENGTH_LONG).show();
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                Log.d(TAG, "onReceive: device=" + device.toString());
            }
        }
    }


    public class RadarAlarmHandler extends Handler {

        private final String TAG = RadarAlarmHandler.class.getSimpleName();


        private RadarAlarmHandler(Looper looper) {
            super(looper);
        }


        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d(TAG, "handleMessage: " + msg.what);
            switch (msg.what) {
                case 0:
                    AOAConnectManager.getInstance().init(MainActivity.this);
                    AOAConnectManager.getInstance().startAOAConnectThread();
                    break;
                case 1:
                    AOAConnectManager.getInstance().stopAOAReadThread();
                    AOAConnectManager.getInstance().stopAOAConnectThread();
                    AOAConnectManager.getInstance().stopSocketReadThread();
                    break;
                case 2:
                    sendProtocolMatchStatus();
                    break;
                case 3:

                    break;
            }
        }

    }

    public void sendProtocolMatchStatus() {
        try {
            CarlifeProtocolVersionProto.CarlifeProtocolVersion protocolVersion
                    = CarlifeProtocolVersionProto.CarlifeProtocolVersion
                    .newBuilder()
                    .setMajorVersion(1)
                    .setMinorVersion(2).build();
            CarlifeCmdMessage protocolM = new CarlifeCmdMessage(true);
            protocolM.setServiceType(CommonParams.MSG_CMD_HU_PROTOCOL_VERSION);
            protocolM.setData(protocolVersion.toByteArray());
            protocolM.setLength(protocolVersion.getSerializedSize());


            byte[] head = protocolM.toByteArray();
            byte[] data = protocolM.getData();
            int i = AOAHostSetup.getInstance().bulkTransferOut(head, head.length, data, data.length);
            Log.d(TAG, "sendProtocolMatchStatus: send code " + i);
            CarlifeCmdMessage carlifeCmdMessage = CarlifeUtil.sendStatisticsInfo(1000);
            int i1 = AOAHostSetup.getInstance().bulkTransferOut(carlifeCmdMessage.toByteArray(), carlifeCmdMessage.toByteArray().length,
                    carlifeCmdMessage.getData(), carlifeCmdMessage.getData().length);
            Log.d(TAG, "sendProtocolMatchStatus: 2 send code " + i1);
        } catch (Exception ex) {
            Log.e(TAG, "sendProtocolMatchStatus fail");
            ex.printStackTrace();
        }
    }
}
